var util = require('../../../utils/util.js')   
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderType: [
      { name: '全部', status: '0' },
      { name: '待付款', status: '1' },
      { name: '设计中', status: '2'},
      { name: '生产中', status:'3'},
      {name:'待收货',status:'4'},
      {name:'待评价',status:'5'},
      {name:'已完成',status:'6'}
    ],
    config:{
      p: 1,
      pageSize: 5,
      nPages: 5,
      // status:'',
      insert_time_search_store:'desc',
      user_id: '',
      // order_on:'',
      pay_status:''
    },
    cookiez: '',
    currentIndex:0,
    orderList: [],
    allPages: null,    // 总页数
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
    searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this
    self.setData({
      currentIndex:options.index
    })
    let config = self.data.config
    config.type = self.data.orderType[options.index].status
    config.pay_status = options.index == 1 ? '0' : ''
    config.user_id = wx.getStorageSync('userInfo').id
    self.setData({
      config: config
    })
    self.getData(self.data.config, wx.getStorageSync('cookiez'))
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var self=this
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this
    self.getData(self.data.config, wx.getStorageSync('cookiez'))
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self = this
    let config = self.data.config
    config.p = 1
    self.setData({
      config: config
    })
    // if (self.data.searchLoading){return}
    self.getData(self.data.config, wx.getStorageSync('cookiez'))
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    var self = this
    if (self.data.searchLoading && !self.data.searchLoadingComplete) {
      let config = self.data.config
      config.p = config.p + 1
      self.setData({
        config: config
      })
      // console.log(self.data.config)
      self.getData(self.data.config, wx.getStorageSync('cookiez'))
    }
  },
  /**
   * 去登录
   */
  login: function () {
    var url = './login/login';
    console.log(url);
    wx.navigateTo({
      url: url
    })
  },
  // 跳转订单详情
  goOrderDetail: function (event) {
    var url = '../orderDetail/orderDetail?id=' + event.currentTarget.dataset.id;
    wx.navigateTo({
      url: url
    })
  },
  // 获取订单列表
  getOrder:function(event){
    let self = this
    self.setData({
      currentIndex: event.currentTarget.dataset.index,
      orderList:[]
    })
    let config=self.data.config
    config.type = event.currentTarget.dataset.status
    config.p = 1
    config.pay_status = event.currentTarget.dataset.index==1?'0':''
    self.setData({
      config: config
    })
    // console.log(self.data.config)
    self.getData(self.data.config, wx.getStorageSync('cookiez'))
  },
  getData: function (params, cookiez){
    var self=this
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: wx.envConfig.host +'/app/order/findOrderListPage?cookiez=' + cookiez,
      data: JSON.stringify(params),
      method: 'POST',
      success: function (res) {
        if (res.data.error_code === "Y10000") {
          wx.hideLoading();
          for (var i = 0; i < res.data.datas.datas.list.length; i++) {
            var element = res.data.datas.datas.list[i];
            console.log('订单数量和价格-------------------的点点滴滴',+res.data.datas.datas.list[i].pay_money, +res.data.datas.datas.list[i].num)
            element.num_pay_money = +res.data.datas.datas.list[i].pay_money
            element.pay_money = util.formatMoney(element.pay_money)
            element.insert_time = util.parseTime(element.insert_time)
            element.orderStatus=wx.filter.status(element.status)
            if (element.order_type == 5) {
              if (element.status == 2) {
                element.orderStatus = '3D打印中';
              }
            }
            if (element.order_type == 6 || element.order_type == 2) {
              if (element.status == 43) {
                element.orderStatus = '待审核分割报告';
              }
            }
            if (element.status == '2' && 　element.is_urgent_order == '1') {
              element.orderStatus = '等待设计师确认';
            }
            if (element.supplement_status == '1' && element.status != '99') {
              element.orderStatus = '您需要补传数据';
            }
            if (element.status == '6' && element.schemeInfo.length>0) {
              if (element.schemeInfo[0].scheme_status == 0 || element.schemeInfo[1].scheme_status == 0) {
                  element.orderStatus = '待审核方案'
              }
            }
            res.data.datas.datas.list[i] = element;
          }
          if (res.data.datas.datas.list.length<5){
            self.setData({
              searchLoadingComplete: true, //把“没有数据”设为true，显示  
              searchLoading: false  //把"上拉加载"的变量设为false，隐藏  
            });  
          }else{
            self.setData({ 
              searchLoading: true, //把"上拉加载"的变量设为false，隐藏
              searchLoadingComplete: false, //把“没有数据”设为true，显示  
            });   
          }
          var orderList = self.data.config.p == 1 ? res.data.datas.datas.list : self.data.orderList.concat(res.data.datas.datas.list)
          console.log('111111', self.data.config.p)
          self.setData({
            orderList: orderList,
            allPages: res.data.datas.datas.total,
          })
        }
        wx.stopPullDownRefresh();
      },
      fail: function ({ errMsg }) {
        wx.stopPullDownRefresh();
        console.log('request fail', errMsg)
      }
    })
  },
  //确认收货
  delivery: function (event) {
    let self = this
    wx.showModal({
      title: '',
      confirmText: '是',
      cancelText: '否',
      cancelColor: '#4825BF',
      confirmColor: '#4825BF',
      content: '您真的要确认收货吗?',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: wx.envConfig.host+'/order/confirmReceiptOrder?params=' + JSON.stringify({ "order_id": event.currentTarget.dataset.id}) + '&cookiez=' + wx.getStorageSync('cookiez'),
            method: 'POST',
            success: function (res) {
              if (res.data.error_code === "Y10000") {
                self.getData(self.data.config, wx.getStorageSync('cookiez'))
              }
            },
            fail: function ({ errMsg }) {
              console.log('request fail', errMsg)
            }
          })
        }
      }
    })
  },
  //查看物流信息
  lookExpressList: function (event) {
    var url = '../orderDetail/expressInfo?id=' + event.currentTarget.dataset.id;
    wx.navigateTo({
      url: url
    })
  },
  //去支付
  goPay:function(e){
    var url = '../../home/category/uploadCTdata/orderpay/orderpay?id=' + e.currentTarget.dataset.id + '&price=' + e.currentTarget.dataset.price;
    wx.navigateTo({
      url: url
    })
  },
  //完善信息
  uploadCtData: function (e) {
    wx.navigateTo({
      url: '../../home/category/uploadCTdata/uploadCTdata?id=' + e.currentTarget.dataset.id
    })
  }
})